INC_PATH ?= $(CURDIR)/include/
LIB_PATH ?= $(CURDIR)/lib/
BIN_PATH ?= $(CURDIR)/bin/
DBG_PATH ?= $(CURDIR)/objs/

SRC_LIB := bsp	

SDL2_LIB := $(wildcard ./lib/*.a) -L$(LIB_PATH)
L_SRC_LIB := -L$(LIB_PATH) $(patsubst %,-l%,$(SRC_LIB))

SRC := $(wildcard *.c) $(wildcard ../*.c)
INC := $(addprefix -I, $(INC_PATH)) -I$(CURDIR)/inlcude -I$(CURDIR)/../ #FIXME
OBJ := $(patsubst %.c,$(DBG_PATH)/%.o,$(notdir $(SRC)))
DEP := $(patsubst %.c,$(DBG_PATH)/%.d,$(notdir $(SRC)))
LIB := $(patsubst %,$(LIB_PATH)/%.a,$(SRC_LIB))

SRC_CFLAGS := -D_GNU_SOURCE -DLINUX=2 -g3 -O2 -Wall -std=gnu99 -march=armv7-a -mfloat-abi=hard -mfpu=neon -ftree-vectorize -ffast-math \
	-DPRODUCT_CLASS=\"$(PRODUCT_CLASS)\" -DPRODUCT_MODEL=\"$(PRODUCT_MODEL)\" -DSOC_MODEL=\"$(SOC_MODEL)\" -DSERISE_CODE=\"$(SERISE_CODE)\"
	
CROSS ?= arm-linux-
CC := $(CROSS)gcc
CXX := $(CROSS)g++
AR := $(CROSS)ar
STRIP := $(CROSS)strip
CFLAGS := $(SRC_CFLAGS) $(INC)
LDFLAGS :=$(L_SRC_LIB) $(SDL2_LIB) -lpthread -lm -lrt -lmembroker -lfreetype

MK_SRC_LIB := make -f $(CURDIR)/Makefile.srclib \
	CC="$(CC)" CXX="$(CXX)" AR="$(AR)" CFLAGS="$(CFLAGS)" \
	INC_PATH="$(INC_PATH)" LIB_PATH="$(LIB_PATH)" BIN_PATH="$(BIN_PATH)" DBG_PATH="$(DBG_PATH)" \
	--directory=$(CURDIR)/

###########################################################
###########################################################
TARGET := $(BIN_PATH)/finder

.PHONY : clean distclean

$(DBG_PATH)/%.o : ../%.c
	@mkdir -p $(shell dirname $@)
	@echo -e "\033[33mmaking $@ $<...\033[0m"
	$(CC) $(CFLAGS) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o $@ -c $<

$(TARGET): $(OBJ)
	$(CC) $^ -o $@ $(LDFLAGS)
	$(STRIP)  $@
	
srclib:
	@for i in $(SRC_LIB) ; do if test -d `dirname $$i` ; \
		then $(MK_SRC_LIB)$$i || exit $$? ; fi ; done

all: srclib $(TARGET)
sinclude $(DEP)

clean:
	@rm -Rf $(DBG_PATH)/* $(TARGET)

distclean: clean
	@rm -Rf $(LIB_OUT)


